package com.common.base.utils

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.annotation.StringRes
import com.common.base.app.extras.isNoEmpty
import com.common.base.databinding.BaseViewToastBinding
import java.lang.Exception

/**
 * toast工具类
 */
object ToastUtils {
    /**
     * 展示一个吐司
     */
    fun showToast(msg: String?, duration: Int) {
        if (msg.isNoEmpty()) {
            AppUtils.post {
                val binding =
                    BaseViewToastBinding.inflate(LayoutInflater.from(AppUtils.getContext()))
                binding.toastTv.text = msg
                val toast = Toast(AppUtils.getContext())
                toast.duration = duration
                toast.setGravity(Gravity.CENTER, 0, 0)
                toast.view = binding.root
                toast.show()
            }
        }
    }

    /**
     * 展示一个吐司，短时间的吐司
     */
    @JvmStatic
    fun showToast(msg: String?) {
        showToast(msg, Toast.LENGTH_SHORT)
    }

    /**
     * 展示一个吐司
     */
    fun showToast(@StringRes strId: Int) {
        val msg = AppUtils.getString(strId)
        showToast(msg, Toast.LENGTH_SHORT)
    }


    /**
     * 长时间展示的吐司
     */
    fun showLong(msg: String?) {
        showToast(msg, Toast.LENGTH_LONG)
    }

    /**
     * 长时间展示的吐司
     */
    fun showLong(@StringRes strId: Int) {
        val msg = AppUtils.getString(strId)
        showToast(msg, Toast.LENGTH_LONG)
    }

    /**
     * 展示一个吐司
     */
    fun toastView(view: View, duration: Int = Toast.LENGTH_LONG, gravity: Int = Gravity.CENTER, xOffset: Int = 0, yOffset: Int = 0) {
        AppUtils.post {
            val toast = Toast(AppUtils.getContext())
            toast.duration = duration
            toast.setGravity(gravity, xOffset, yOffset)
            toast.view = view
            toast.show()
        }
    }
}